import logging
from functools import wraps
from flask import session, g
from info.models import User


def do_index_class(index):
    """自定义过滤器，过滤点击排序html的class"""
    if index == 1:
        return "first"
    elif index == 2:
        return "second"
    elif index == 3:
        return "third"
    else:
        return ""


def user_login_data(view_func):
    """装饰器外层函数"""
    @wraps(view_func)
    def wrapper(*args, **kwargs):
        """装饰器内层函数"""
        user_id = session.get('user_id')
        user = None
        if user_id:
            try:
                user = User.query.get(user_id)
            except Exception as e:
                logging.error(e)

        g.user = user
        return view_func(*args, **kwargs)

    return wrapper